package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.VendorEmployeeMapper;
import org.linlinjava.litemall.db.dao.VendorMapper;
import org.linlinjava.litemall.db.dao.VendorMapperExt;
import org.linlinjava.litemall.db.domain.Vendor;
import org.linlinjava.litemall.db.domain.Vendor.Column;
import org.linlinjava.litemall.db.domain.VendorEmployee;
import org.linlinjava.litemall.db.domain.VendorEmployeeExample;
import org.linlinjava.litemall.db.domain.VendorExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class VendorService {

    @Resource
    private VendorMapper vendorMapper;
    @Resource
    private VendorMapperExt vendorMapperExt;
    @Resource
    private VendorEmployeeMapper vendorEmployeeMapper;

    public List<Vendor> query(String vendorCode, String vendorName, Integer status, Integer offset, Integer limit, String sort, String order) {
        VendorExample example = new VendorExample();
        VendorExample.Criteria criteria = example.or();

        if (!StringUtils.isEmpty(vendorCode)) {
            criteria.andCodeLike("%" + vendorCode + "%");
        }
        if (!StringUtils.isEmpty(vendorName)) {
            criteria.andNameLike("%" + vendorName + "%");
        }
        if (null != status) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(offset, limit);

        return vendorMapper.selectByExample(example);
    }

    public List<Vendor> getByRegion(String regionCode) {
        VendorExample example = new VendorExample();
        example.or()
                .andRegionCodeEqualTo(regionCode)
                .andDeletedEqualTo(false);
        return vendorMapper.selectByExample(example);
    }

    public List<Vendor> getAll() {
        VendorExample example = new VendorExample();
        example.or()
               .andDeletedEqualTo(false);
        return vendorMapper.selectByExample(example);
    }

    /**
     * 获取某个商铺信息,包含完整信息
     *
     * @param id
     * @return
     */
    public Vendor findById(Long id) {
        VendorExample example = new VendorExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return vendorMapper.selectOneByExample(example);
    }

    public int updateById(Vendor vendor) {
        vendor.setUpdateTime(LocalDateTime.now());
        return vendorMapper.updateByPrimaryKeySelective(vendor);
    }

    public int updateByVersion(Vendor vendor) {
        Integer version = vendor.getVersion();

        VendorExample example = new VendorExample();
        example.or()
                .andIdEqualTo(vendor.getId())
                .andVersionEqualTo(version);

        vendor.setVersion(version + 1);
        vendor.setUpdateTime(LocalDateTime.now());
        return vendorMapper.updateByExampleSelective(vendor, example);
    }

    public void deleteById(Long id) {
        vendorMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(Vendor vendor) {
        vendor.setAddTime(LocalDateTime.now());
        vendor.setUpdateTime(LocalDateTime.now());
        vendorMapper.insertSelective(vendor);
    }

    /**
     * 根据商铺ID获取所有雇员
     * @param vendorId
     * @return
     */
    public List<VendorEmployee> getEmployees(Long vendorId){
        VendorEmployeeExample example = new VendorEmployeeExample();
        example.or().andVendorIdEqualTo(vendorId).andDeletedEqualTo(false);
        return vendorEmployeeMapper.selectByExample(example);
    }

    public void deleteEmployeeById(Long id) {
        vendorEmployeeMapper.deleteByPrimaryKey(id);
    }
    public void addEmployee(VendorEmployee employee) {
        employee.setAddTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        vendorEmployeeMapper.insertSelective(employee);
    }

    public int updateEmployeeById(VendorEmployee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return vendorEmployeeMapper.updateByPrimaryKeySelective(employee);
    }
}
